--#include "data\config\misc\MoneyTreeConfig.lua" once

TreeTypeToDailyType = 
{
	[1] = enDailyOpTimes_MoneyTree,
	[2] = enDailyOpTimes_MoneyTree_BindYb,
}
DailyTypeToTreeCfg = 
{
	[enDailyOpTimes_MoneyTree] = MoneyTreeCfg[1],
	[enDailyOpTimes_MoneyTree_BindYb] = MoneyTreeCfg[2],
}

--设置个人摇钱树操作次数
function SetPersonalMoneyTreeTimes( sysarg, nTreeType )
	AddActorDailyDoneTimes( sysarg, TreeTypeToDailyType[nTreeType], 0, 1 )		--参与次数+1
end

--获取个人摇钱树已操作次数
function GetPersonalMoneyTreeTimes( sysarg, nTreeType)
	local doneTimes = GetActorDailyDoneTimes( sysarg, TreeTypeToDailyType[nTreeType], 0 )
	--print("GetPersonalMoneyTreeTimes Times= "..doneTimes)
	return doneTimes
end

function GetOpenServerDayMoneyTreeCfg(nTreeType)
	if MoneyTreeCfg[nTreeType] and IsOpenServerDayInRange(MoneyTreeCfg[nTreeType].openServerDay) then
		return MoneyTreeCfg[nTreeType]
	end
end
function IsMoneyTreePlayerLevelConform( sysarg, nTreeType)
	local elem = GetOpenServerDayMoneyTreeCfg(nTreeType)
	if not elem then
		return false
	end
	if not Actor.checkActorLevel( sysarg, elem.LevelLimit[2], elem.LevelLimit[1] ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016, ttFlyTip)
		return false
	end
	return true
end


--判定双倍奖励
function JudgeMoneyTreeDoubleAwards( sysarg , nTreeType )
	local elem = GetOpenServerDayMoneyTreeCfg(nTreeType )
	if not elem then
		return false
	end
	local critRate = elem.critRate * 100
	local randomNum = System.getRandomNumber(100)
	--print("JudgeMoneyTreeDoubleAwards critRate= "..critRate.." randomNum= "..randomNum)
	if critRate >= randomNum then
		return true
	end
	return false
end

--奖励获取后提示
--moneyType: mtYuanbao,mtBindYuanbao,mtBindCoin
function MoneyTreeAwardTips( sysarg , moneyType, value)
	local msg = ""
	if moneyType == mtYuanbao then			    --元宝
		msg = sysarg,Lang.ScriptTips.zf302
	elseif moneyType == mtBindYuanbao then		--绑元
		msg = sysarg,Lang.ScriptTips.zf301
	elseif moneyType == mtBindCoin then		    --金币
		msg = sysarg,Lang.ScriptTips.zf300
	end
	if msg then
		string.format( msg, value )
		Actor.sendTipmsg(sysarg, msg, ttFlyTip)
	end
end

--是否达到最大次数（vipConfig配置获得）
function IsMoneTreePerMaxTimes( sysarg , nTreeType)
	if CheckActorDailyDoneTimes( sysarg, TreeTypeToDailyType[nTreeType], 0 ) then
		return false
	end

	return true
end


--获取摇钱树奖励数据
function GetMoneyTreeAwards( sysarg, nTreeType )
	local dVal = Actor.getDyanmicVar(sysarg)
	if not dVal.MoneyTreeAwards then
		 dVal.MoneyTreeAwards = ""
	end
	local MoneyTreeAwards = nil
	local elem = GetOpenServerDayMoneyTreeCfg(nTreeType)
	if not elem then
		return nil
	end

	local operTimes = GetPersonalMoneyTreeTimes(sysarg, nTreeType) + 1
	local awards = elem.awards[operTimes] 
	if not awards and operTimes > #elem.awards then 
		awards = elem.awards[#elem.awards]
	end
	
	local multiple = 1
	if JudgeMoneyTreeDoubleAwards(sysarg, nTreeType) then
		multiple = elem.multiple
	end

	if awards then
		MoneyTreeAwards = DeepCopyFromObject( awards )  --对奖励内容进行深拷贝
		for i,oneAward in ipairs( MoneyTreeAwards ) do
			oneAward.count = oneAward.count * multiple
			oneAward.multiple = multiple == 1 and 0 or 1 
		end
	end
	
	dVal.MoneyTreeAwards = BaseFuc_serialize(MoneyTreeAwards)

	return MoneyTreeAwards
end
--判断是否为免费次数
function IsMoneyTreeFreeTimes( sysarg, nTreeType )
	local elem = GetOpenServerDayMoneyTreeCfg(nTreeType)
	if not elem then
		return false
	end

	local operTimes = GetPersonalMoneyTreeTimes(sysarg, nTreeType) + 1
	for k,v in pairs(elem.operateTimesCfg.freeTimes) do
		if operTimes == v then
			return true
		end
	end
	return false
end

function GetMoneyTreeConsume( sysarg, nTreeType )
	local elem = GetOpenServerDayMoneyTreeCfg(nTreeType)
	if not elem then
		return nil
	end
	local operTimes = GetPersonalMoneyTreeTimes(sysarg, nTreeType) + 1
	local consumes = elem.consumes[operTimes]
	if not consumes and operTimes > #elem.consumes then
		consumes = elem.consumes[#elem.consumes]
	end
	return consumes
end

--处理消费
--返回： false：失败   true：成功
function IsMoneyTreeConsumesEnough( sysarg, nTreeType)
	local elem = GetOpenServerDayMoneyTreeCfg(nTreeType)
	if not elem then
		return false
	end
	
	if IsMoneyTreeFreeTimes( sysarg, nTreeType ) then
		return true
	end

	local consumes = GetMoneyTreeConsume( sysarg, nTreeType)
	if not consumes then
		return false
	end
	
	--判断费用是否足够
	if not CheckConsumeCond( sysarg, consumes, 1 ) then
		return false
	end

	
	return true
end

function GiveMoneyTreeAwardsToPlayer( sysarg, nTreeType )
	local awards = GetMoneyTreeAwards( sysarg, nTreeType) 
	if not awards then
		return false
	end

	local elem = GetOpenServerDayMoneyTreeCfg( nTreeType )
	if not elem then
		return false
	end
	
	local consumes = GetMoneyTreeConsume( sysarg, nTreeType )
	if not consumes then
		return false
	end

	--为防止奖励金额超限，此处添加金额判断
	local dVal = Actor.getDyanmicVar(sysarg)
	if not dVal.MoneyTreeAwards then
		return false
	end 
	local MoneyTreeAwards = BaseFuc_unserialize(dVal.MoneyTreeAwards)
	if not MoneyTreeAwards then
		return false
	end
	local personalMoneyCnt = Actor.getMoneyCount(sysarg, elem.moneyType)
	if elem.moneyType == mtBindCoin and personalMoneyCnt == GlobalConfig.bindCoinLimit  then
		Actor.sendTipmsg(sysarg , Lang.ScriptTips.COM0106, ttFlyTip) 
		return false
	end 
	if elem.moneyType == mtBindYuanbao and personalYunBaoCnt == GlobalConfig.bindYuanbaoLimit then
		Actor.sendTipmsgWithId(sysarg , Lang.ScriptTips.COM0105, ttFlyTip) 
		return false
	end

	for k,oneAward in pairs(MoneyTreeAwards) do
		personalMoneyCnt = personalMoneyCnt + oneAward.count
		if oneAward.type == qatBindMoney  then
			if personalMoneyCnt == GlobalConfig.bindCoinLimit  then
				Actor.sendTipmsg(sysarg , Lang.ScriptTips.COM0106, ttFlyTip) 
				return false
			end
		elseif oneAward.type == qatBindYb then
			if personalYunBaoCnt == GlobalConfig.bindYuanbaoLimit then
				Actor.sendTipmsgWithId(sysarg , Lang.ScriptTips.COM0105, ttFlyTip) 
				return false
			end
		end 
	end
	
	
	--扣除费用
	if not CheckConsumeCond( sysarg, consumes, 1, false ) then
		return false
	end
	if not DoConsumeCond( sysarg, consumes, 1, LogIds.MoneyTreeLogId, Lang.ScriptTips.MoneyTreeLog01 or "") then
		return false
	end
	--给予奖励
	if not Awards.Give( sysarg, awards,  LogIds.MoneyTreeLogId, Lang.LogTips.MoneyTree ) then
		return false
	end
	
	return true
end

function OnDealMoneyTreeOperate( sysarg, nTreeType )
	--print("OnDealMoneyTreeOperate ...")
	if not IsMoneyTreePlayerLevelConform( sysarg, nTreeType ) then
		return
	end

	if IsMoneTreePerMaxTimes( sysarg, nTreeType ) then  
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MoneyTreeMsg01, ttFlyTip)
		return 
	end

	if not IsMoneyTreeConsumesEnough( sysarg, nTreeType ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MoneyTreeMsg02, ttFlyTip)
		return
	end

	if not GiveMoneyTreeAwardsToPlayer( sysarg, nTreeType ) then
		return
	end

	local dVal = Actor.getDyanmicVar(sysarg)
	if not dVal.MoneyTreeAwards then
		return
	end 
	SetPersonalMoneyTreeTimes( sysarg, nTreeType )

	local npack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sMoneyTreeData)
	if not npack then
		return
	end
	local MoneyTreeAwards = {}
	
	MoneyTreeAwards = BaseFuc_unserialize(dVal.MoneyTreeAwards)
	local times = GetPersonalMoneyTreeTimes(sysarg, nTreeType)
	DataPack.writeByte( npack,  nTreeType )
	DataPack.writeByte( npack,  times )
	DataPack.writeByte( npack,  #MoneyTreeAwards )
	for i,oneAward in ipairs( MoneyTreeAwards ) do
		DataPack.writeByte( npack, oneAward.multiple )
		DataPack.writeByte( npack, oneAward.type )
		DataPack.writeUint64( npack, oneAward.count )
	end
	DataPack.flush(npack)
end

function OnDealMoneyTreeTimesRequest( sysarg, nTreeType)
	--print("OnDealMoneyTreeTimesRequest ...")
	local npack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sMoneyTreeData)
	if not npack then
		return
	end
	
	local times = GetPersonalMoneyTreeTimes(sysarg, nTreeType)
	DataPack.writeByte( npack,  nTreeType )
	DataPack.writeByte( npack,  times )
	DataPack.writeByte( npack,  0 )
	DataPack.flush(npack)
end


MiscsImplement.onClickMoneyTree = function( sysarg, msgid, packet )
	--print("OnMoneyTreeClick ...")
	if msgid ~= LogicCmd.miscSys.sub.cMoneyTreeData then
		return
	end
	local nOptType = DataPack.readByte(packet)
	local nTreeType = DataPack.readByte(packet)
	if nOptType == 0 then
		OnDealMoneyTreeTimesRequest(sysarg, nTreeType)
	elseif nOptType == 1 then 
		OnDealMoneyTreeOperate(sysarg,nTreeType)
	end
end

MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cMoneyTreeData, MiscsImplement.onClickMoneyTree)
